package com.woniuxy.three.pms.sms;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.woniuxy.three.pms.util.CacheUtil;
import com.woniuxy.three.pms.util.Msg;
import lombok.extern.slf4j.Slf4j;

/**
 *
 * @ProjectName:Pms
 * @Date:2019年6月18日
 * @ClassName:UserController.java
 * @Author:ThinkPad
 * @Description:TODO 验证短信验证码是否正确
 * @Version:1.0
 * @See：参考版本
 *
 */
@SuppressWarnings("deprecation")
@RestController
@Slf4j
public class CheckSmsResult {


	//缓存工具
	@Autowired
	private CacheUtil cacheUtil;

	//注入发送短信的对象
	@Autowired
	private SmsSender smsSender;

	//校验是否可以发送验证码
	@RequestMapping("/verify")
	public Msg checkVerifyCode(String mobile,HttpServletRequest request) {
		try{
			if (StringUtils.isBlank(mobile)){
				throw new Exception();
			}else {
				if (mobile.length() != 11) {
					return Msg.fail().add("result", "手机号不合法，长度只能为11位");
				}
				String ip = CacheUtil.getIpAddress(request);
				int checkResult = cacheUtil.checkVerifyStatus(mobile,ip);
				if (checkResult ==1) {
					return Msg.fail().add("result", "验证码未失效");
				}else if (checkResult == 2) {
					return Msg.fail().add("result", "当前用户的验证码请求次数达到上限");
				}else if(checkResult == 3) {
					return Msg.fail().add("result", "当前IP的请求次数上限");
				}
				String cacheResult = cacheUtil.getValueWithKey(mobile);
				if (StringUtils.isBlank(cacheResult)) {
					//发送短信
					String newVerifyCode = RandomStringUtils.randomAlphanumeric(4);
					//存入缓存
					cacheUtil.cacheWithTimeOut(mobile, newVerifyCode, Duration.of(SmsSender.SMS_VERIFICATION_CODE_EXPIRATION_TIME, ChronoUnit.MINUTES));
					//设置用户发送数量
					String mobileConutValue =  cacheUtil.getValueWithKey("login_"+mobile);
					if (!StringUtils.isBlank(mobileConutValue)) {
						int count = Integer.parseInt(mobileConutValue);
						count ++;
						cacheUtil.cacheWithKeyValue("login_"+mobile, count+"");
					}else {
						cacheUtil.cacheWithTimeOut("login_"+mobile, "1", Duration.of(1, ChronoUnit.DAYS));
					}
					//设置IP发送数量
					String ipCount = cacheUtil.getValueWithKey("login_"+ip);
					if (!StringUtils.isBlank(ipCount)) {
						int count = Integer.parseInt(ipCount);
						count++;
						cacheUtil.cacheWithKeyValue("login_"+ip, count);
					}else{
						cacheUtil.cacheWithTimeOut("login_"+ip,"1", Duration.of(1, ChronoUnit.DAYS));
					}
					smsSender.sendVercode(mobile, SmsSender.TEMP_SEND_VERIFY_CODE, newVerifyCode+","+SmsSender.SMS_VERIFICATION_CODE_EXPIRATION_TIME);
					return Msg.success().add("result", "已发送，请注意查收短信");
				}
			}
		} catch (Exception e) {
			log.error("您的手机号或者验证码不正确");
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Msg.fail();
		}
		return Msg.fail();
	}

}